﻿using Common.Result;
using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.AddressBook;
using Service;

namespace TakeawayWebApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class AddressBookController : ControllerBase
    {
        private readonly IAddressBookService _addressBookService;
        private readonly ILogger<AddressBookController> _logger;
        public AddressBookController(IAddressBookService addressBookService, ILogger<AddressBookController> logger)
        {
            _addressBookService = addressBookService;
            _logger = logger;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="addressBookAdd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Save(AddressBookAdd addressBookAdd)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            addressBookAdd.UserId = userId;
            await _addressBookService.SaveAsync(addressBookAdd);
            return ResultHelper.Success("添加成功");
        }
        /// <summary>
        /// 设置默认地址
        /// </summary>
        /// <param name="addressBookEdit"></param>
        /// <returns></returns>
        [HttpPut("default")]
        public async Task<ApiResult> SetDefault(AddressBookEdit addressBookEdit)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            addressBookEdit.UserId = userId;
            await _addressBookService.SetDefaultAsync(addressBookEdit);
            return ResultHelper.Success("设置成功");
        }
        /// <summary>
        /// 根据id查询地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ApiResult> GetAddressBookById(long id)
        {
            return ResultHelper.Success(await _addressBookService.GetAddressBookByIdAsync(id));
        }
        /// <summary>
        /// 查询默认地址
        /// </summary>
        /// <returns></returns>
        [HttpGet("default")]
        public async Task<ApiResult> GetDefault()
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res= await _addressBookService.GetDefaultAsync(userId);
            if (res != null) return ResultHelper.Success(res);
            return ResultHelper.Error("没有找到该对象");
        }
        /// <summary>
        /// 查询指定用户的全部地址
        /// </summary>
        /// <param name="addressBookEdit"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<ApiResult> List()
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res = await _addressBookService.GetAddressBooksAsync(userId);
            return ResultHelper.Success(res);
        }
    }
}
